<template>
	<div style="width: 1300px;">
		<el-button type="primary" 
			@click="addBrand"
			size="mini">
			添加品牌
		</el-button>
		<br /> <br>
		
		<el-table  :data="list"
			 border  
			 style="width: 100%"
			>
			<el-table-column   align="center"    prop="brand_name"    label="品牌名称"></el-table-column>
			<el-table-column  label="品牌图片"   align="center">
			     <template slot-scope="scope">
			        <div>
						<el-image style="width: 100px; height: 50px"
						:preview-src-list="[scope.row.img_url]"
						:src="scope.row.img_url"></el-image>
					</div>
			     </template>
			 </el-table-column>
			 <!-- <el-table-column  label="状态"   align="center">
			      <template slot-scope="scope">
			         <div>
						<el-tag v-if="scope.row.status==0" size="small" effect="dark" type="danger">禁用</el-tag>
						<el-tag v-if="scope.row.status==1" size="small" effect="dark">启用</el-tag>
			 		</div>
			      </template>
			  </el-table-column> -->
			  <el-table-column   align="center"   label="状态">
			  	<template slot-scope="scope">
			  	   <div>
			  			<el-switch
						   @change="sortStatusList(scope.row)"	
			  			  v-model="scope.row.status"
			  			 :inactive-value="'0'"
			  			 :active-value="'1'"
			  			 >
			  			</el-switch>
			  	   </div>
			  	</template>
			  </el-table-column>
			  <el-table-column  label="排序(数字大的在前边)" width="200"  align="center">
			       <template slot-scope="scope">
			          <div>
						  <el-input @blur="sortStatusList(scope.row)" size="small" v-model="scope.row.sort"
						  type="number" style="width: 80px;"></el-input>
			  		  </div>
			       </template>
			   </el-table-column>
			<el-table-column   align="center"    prop="add_time"    label="添加时间"></el-table-column>
			<el-table-column   align="center"    prop="update_time"    label="修改时间"></el-table-column>
			
		   <el-table-column  label="操作"   align="center">
		        <template slot-scope="scope">
		            <el-button type="text" @click="editRow(scope.row)">编辑</el-button>
					<el-button type="text" 
						v-if="scope.row.is_default==0"
						@click="setDefoult(scope.row)">设为默认</el-button>
					<el-button type="text" @click="delRow(scope.row.id)">删除</el-button>
		        </template>
		    </el-table-column>
		</el-table>
		<div class="page">
		    <el-pagination
		            background
		            @size-change="pageChange"
		            @current-change="handleCurrentChange"
		            :current-page="page"
		            :page-size="pageSize"
		            layout="total, sizes, prev, pager, next, jumper"
		            :total="page_totle">
		    </el-pagination>
		</div>
		<el-dialog
		        :title="addTitle"
				:close-on-click-modal="false"
				:modal-append-to-body="false"
				:append-to-body="true"
		        :visible.sync="formShow"
		        width="1000px"
		>
		  <div>
			  <el-form ref="form" label-width="80px">
				  <el-form-item label="品牌名称">
				  		<el-input v-model="form.brand_name" size="small"></el-input>
				  </el-form-item>
				  <el-form-item label="品牌排序">
				  		<el-input v-model="form.sort" type="number" 
							size="small"></el-input>
				  </el-form-item>
				  <el-form-item label="品牌图片">
				  		<el-button size="mini" @click="isShow=true" type="primary">选择图片</el-button>
				  </el-form-item>
				  <el-form-item v-if="form.img_url">
					  <div style="display: flex;">
						  <div style="position: relative;margin-right: 10px;">
							  <img style="width: 100px;height: 100px;" :src="form.img_url" alt="">
							  <i @click="delImg(i)" class="el-icon-circle-close" style="font-size: 20px;position: absolute;top: 0;right:0;cursor: pointer;"></i>
						  </div>
					  </div>
				  </el-form-item>
				  <el-form-item >
				      <el-button type="primary" @click="confirm" size="small">确定</el-button>
				  </el-form-item>
			  </el-form>
		  </div>
		</el-dialog>
		
		<el-dialog title="选择图片"
				:close-on-click-modal="false"
				:modal-append-to-body="false"
				:append-to-body="true"
		        :visible.sync="isShow"
		        width="1050px">
			 <div style="padding-bottom: 30px;">
				 <imgSys @getImgData="getImgData" />
			 </div>
		     
		</el-dialog>
	</div>
</template>

<script>
  import imgSys from '@/components/imgSys'
	export default {
		data(){
			return {
				title:"",
				page:1,
				pageSize:10,
				form:{
					id:null,
					img_url:'',
					is_default:"0",
					brand_name:'',
					old_brand:'',
					sort:1
				},
				addTitle:'新增',
				isShow:false,
				table_name:'hs_brand'
			}
		},
		components:{
			imgSys
		},
		mounted() {
			this.lodeList()
		},
		methods:{
			addBrand(){
				this.form = {
					id:null,
					img_url:'',
					is_default:"0",
					brand_name:'',
					old_brand:'',
					sort:1
				}
				this.addTitle = '新增'
				this.formShow = true
			},
			delRow(id){
				this.$confirm('确定删除', '提示', {
				          confirmButtonText: '确定',
				          cancelButtonText: '取消',
				          type: 'warning'
				        }).then(() => {
							let params = {
								table_name:this.table_name,
								ids:[id]
							}
				          this.$post('/prodectSys/delProdect',params,res=>{
							  this.lodeList()
						  })
				        })
			},
			sortStatusList(row){
				let params = {
					id:row.id,
					sort:row.sort,
					status:row.status,
					table_name:this.table_name
				}
				this.$post('/prodectSys/updateSortStatus',params,res=>{
					this.$message.success('操作成功')
					this.lodeList()
				})
			},
			setDefoult(row){
				let params = {
					id:row.id,
					is_default:'1',
					img_url:row.img_url
				}
				this.$post('/prodectSys/setDefult',params,res=>{
					this.$message.success('操作成功')
					this.lodeList()
				})
			},
			getImgData(val){
				this.form.img_url = val
				this.isShow = false
			},
			editRow(row){
				this.addTitle = '编辑'
				for(let i in this.form){
					this.form[i] = row[i]
				}
				this.form.old_brand = row.brand_name
				this.formShow = true
			},
			clearSearch(){
				this.title = ''
				this.search()
			},
			confirm(){
				let url
				if(this.form.id){
					url = '/prodectSys/editBanner'
				}else{
					url = '/prodectSys/addBrand'
				}
				this.$post(url,this.form,res=>{
					if(res.data.code==200){
						this.$message.success('操作成功')
						this.formShow = false
						this.lodeList()
					}
				})
			},
			lodeList(){
				let params = {
					page:this.page,
					pageSize:this.pageSize,
					table_name:this.table_name
				}
				this.$post('/prodectSys/getBrandList',params,res=>{
					let data = res.data.data
					this.page_totle = data.count
					this.list = data.result
				})
			},
		}
	}
</script>

<style>
</style>